﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//
using System.Runtime.InteropServices;

namespace StreamingServer
{
    class HikServer
    {
        #region 回调函数结构定义
      public struct callbackinfo
        {
            bool badd;
            string clientip;//客户端ip
            short port;
            string url;//连接地址
        };
        #endregion
        /// <summary>
        /// VC++Demo：委托内调用CBF_NotifyANewConnectioncallback(bool badd,string clientip, short port, string url);;
        ///VC++ 转成C#时定义的回调函数
        /// </summary>
        /// <param name="badd"></param>
        /// <param name="clientip"></param>
        /// <param name="port"></param>
        /// <param name="url"></param>
        public delegate void CBF_NotifyANewConnectioncallback(int badd,string clientip, short port, string url);

        #region 海康sever.dll
        /// <summary>
        /// 初始化，该函数需要在窗口程序初始化时调用
        /// </summary>
        /// <returns>成功返回0，失败返回-1</returns>
        [DllImport(@"\HK\Server.dll")]
        public static extern int InitStreamServerLib();

        /// <summary>
        /// 反初始化，该函数需要在窗口程序关闭时调用
        /// </summary>
        /// <returns>成功返回0，失败返回-1</returns>
        [DllImport(@"\HK\Server.dll")]
        public static extern int FiniStreamServerLib();

        /// <summary>
        /// 初始化服务器端
        /// </summary>
        /// <param name="path">指向流媒体文件所在的目录路径</param>
        /// <param name="port">服务器的侦听端口号，默认值为554</param>
        /// <returns>成功返回0，失败返回-1</returns>
        [DllImport(@"\HK\Server.dll")]
        public static extern int StartServer(string path, short port);

        /// <summary>
        /// 未使用
        /// </summary>
        /// <returns></returns>
        [DllImport(@"\HK\Server.dll")]
        public static extern int RunServer();

        /// <summary>
        /// 未使用
        /// </summary>
        /// <returns></returns>
        [DllImport(@"\HK\Server.dll")]
        public static extern int RunServerLoop();

        /// <summary>
        /// 停止服务毒案，释放资源
        /// </summary>
        /// <returns>成功返回0，失败返回-1</returns>
        [DllImport(@"\HK\Server.dll")]
        public static extern int StopServer();

        /// <summary>
        /// 未使用
        /// </summary>
        /// <returns></returns>
        [DllImport(@"\HK\Server.dll")]
        public static extern int GetServerState();

        /// <summary>
        /// 返回当前连接的客户端数目
        /// </summary>
        /// <returns>当前连接的客户端数目</returns>
        [DllImport(@"\HK\Server.dll")]
        public static extern int GetCurConnection();

        /// <summary>
        /// 返回当前连接的设备通道数
        /// </summary>
        /// <returns>当前连接的设备通道数</returns>
        [DllImport(@"\HK\Server.dll")]
        public static extern int GetCurChannel();

        /// <summary>
        /// 创建播放vod文件必须的索引文件
        /// </summary>
        /// <param name="filename">媒体文件路径</param>
        /// <param name="len">为filename的长度</param>
        /// <returns>成功返回0，失败返回-1</returns>
        [DllImport(@"\HK\Server.dll")]
        public static extern int VodCreateIndex(IntPtr filename, int len);

        /// <summary>
        /// 设置客户端连接/断开消息回调函数
        /// 11.SERVER_API int __stdcall SetNewConnectionCallBack(CBF_NotifyANewConnection callback)
        ///返回值：返回 0。
        ///回调函数：回调函数形式为：
        ///typedef int (__stdcall *CBF_NotifyANewConnection)(bool badd,const char* clientip,unsigned short port, const char* url)
        ///其中badd 为false 表示断开一Client 连接，此时URL 为NULL，true 表示增加一个Client。
        /// </summary>
        /// <param name="badd">fasle表示断开一个客户，true表示新增一个客户</param>
        /// <param name="port">端口</param>
        /// <param name="url">地址，当badd=false时，值为null</param>
        [DllImport(@"\HK\Server.dll")]
        public static extern int  SetNewConnectionCallBack(CBF_NotifyANewConnectioncallback Callback); 
        #endregion       
    }
}
/*
      C++            C#
    =====================================
    WORD              ushort
    DWORD             uint
    UCHAR             int/byte   大部分情况都可以使用int代替,而如果需要严格对齐的话则应该用bytebyte 
    UCHAR*            string/IntPtr
    unsigned char*    [MarshalAs(UnmanagedType.LPArray)]byte[]/？（Intptr）
    char*             string
    LPCTSTR           string
    LPTSTR            [MarshalAs(UnmanagedType.LPTStr)] string
    long              int
    ulong             uint
    Handle            IntPtr
    HWND              IntPtr
    void*             IntPtr
    int               int
    int*              ref int
    *int              IntPtr
    unsigned int      uint
    COLORREF          uint 
 */
